﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CoolERP_BLL;
using CoolERP_Entities;
using CoolERP_Common;

namespace CoolERP_Web.User
{
    public partial class YSKZD_TO_SKD : BasePage
    {
        private Guid XSHTID
        {
            get { return this.GetUrlParam("id") == null ? Guid.Empty : new Guid(this.GetUrlParam("id").ToString()); }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindDDLFPLX();
                BindData();
            }
        }
        //初始化发票类型下拉框
        private void BindDDLFPLX()
        {
            using (CS_FPLXBLL cB = new CS_FPLXBLL())
            {
                this.ddlFPLX.DataSource = cB.GetAll();
                this.ddlFPLX.DataValueField = "BH";
                this.ddlFPLX.DataTextField = "MC";
                this.ddlFPLX.DataBind();
                this.ddlFPLX.Items.Insert(0, "请选择");
                this.ddlFPLX.SelectedIndex = 0;
            }
        }
        private void BindData()
        {
            string xshtBH = this.GetUrlParam("bh").ToString();
            string khJC = this.GetUrlParam("khjc").ToString();
            if (XSHTID != Guid.Empty)
            {
                if (!string.IsNullOrEmpty(xshtBH))
                    this.tbXSHTBH.Text = xshtBH.Trim();
                if (!string.IsNullOrEmpty(khJC))
                    this.tbKHJC.Text = khJC.Trim();
                using (XSHTBLL xshtB = new XSHTBLL())
                {
                    XSHT entityXSHT = xshtB.GetEntity(t => t.ID == XSHTID);
                    this.tbCHSJ.Text = entityXSHT.CHSJ.Value.ToString().Trim();
                    this.tbDHSJ.Text = entityXSHT.DHSJ.Value.ToString().Trim();
                }
                using (XSHTMXBBLL xshtmxB = new XSHTMXBBLL())
                {
                    double sum = Convert.ToDouble(xshtmxB.GetList(t => t.XSHTID == XSHTID).Sum(t => t.DJ * t.SL));
                    this.tbYSZE.Text = sum.ToString();
                }
            }
            else return;
        }

        protected void btnSave_Click(object sender, ImageClickEventArgs e)
        {
            bool isAdd = true;
            bool result = false;
            if (string.IsNullOrEmpty(tbCKSJ.Text.Trim()))
            {
                ShowMessage("催款时间为必填项，请确认无误后再保存！");
                this.tbCKSJ.Focus();
                return;
            }
            if (Convert.ToInt32(ddlFPLX.SelectedIndex) == 0)
            {
                ShowMessage("请选择发票类型");
                this.ddlFPLX.Focus();
                return;
            }
            using (CW_YSKZDBLL yskzdB = new CW_YSKZDBLL())
            {
                CW_YSKZD entity = new CW_YSKZD();
                if (XSHTID != Guid.Empty)
                {
                    XSHTBLL xshtB = new XSHTBLL();
                    XSHT xsht = xshtB.GetEntity(t => t.ID == XSHTID);
                    entity.ID = Guid.NewGuid();
                    ZL_YWLSH ywItem = new ZL_YWLSH();
                    entity.BH = GenYWLSH.GenBH(EnumHelper.GetDescriptionByName(typeof(EnumYWDJLX), EnumYWDJLX.YSKZD), out ywItem, out isAdd);
                    entity.XSHTID = XSHTID;
                    entity.CZRYID = this.currentUser.UserID;
                    entity.CJSJ = DateTime.Now;
                    entity.CKSJ = DateTime.Parse(this.tbCKSJ.Text.Trim());
                    entity.YSZE = Convert.ToDouble(this.tbYSZE.Text.Trim());
                    entity.ZDZT = 0;
                    entity.YISHOUZE = 0;
                    entity.FPLXBH = int.Parse(ddlFPLX.SelectedValue);
                }
                try
                {
                    result = yskzdB.Insert(entity);
                }
                catch (Exception ex)
                {
                    ShowMessage(ex.Message);
                }
            }
            if (result)
            {
                lbResult.Text = "保存成功";
            }
            else lbResult.Text = "保存失败";
        }
    }
}